// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   Dwin.java

package shanghaielectric_lib.Utils.dwin;

//import org.apache.http.util.EncodingUtils;

import java.io.DataOutputStream;
import java.util.ArrayList;
import java.util.Map;


//控制全屏，去掉返回键
public class Dwin
{

    public Dwin()
    {
    }

    public static Dwin getInstance()
    {
        return dwin = dwin != null ? dwin : new Dwin();
    }

    public boolean setBrightness(int bright)
    {
        try
        {
            String command[] = {
                "su", "-c", (new StringBuilder("echo ")).append(bright).append(" > /sys/devices/platform/pwm-backlight.0/backlight/pwm-backlight.0/brightness").toString()
            };
            exec(command);
        }
        catch(Exception e)
        {
            return false;
        }
        return true;
    }

    public boolean hideNavigation()
    {
        try
        {
            String procId = android.os.Build.VERSION.SDK_INT <= 14 ? "79" : "42";
            String command[] = {
                "su", "-c", (new StringBuilder("service call activity ")).append(procId).append(" s16 com.android.systemui").toString()
            };
            exec(command);
        }
        catch(Exception e)
        {
            return false;
        }
        return true;
    }

    public boolean showNavigation()
    {
        try
        {
            String command[] = {
                "am", "startservice", "-n", "com.android.systemui/.SystemUIService"
            };
            exec(command);
        }
        catch(Exception e)
        {
            return false;
        }
        return true;
    }

    public boolean setIp(String IP)
    {
        try
        {
            String command[] = {
                "su", "-c", (new StringBuilder("ifconfig eth0 ")).append(IP).append(" netmask 255.255.255.0 up").toString()
            };
            exec(command);
        }
        catch(Exception e)
        {
            return false;
        }
        return true;
    }
//
//    public String getChipID()
//    {
//        String res = null;
//        String fileName = "/sys/devices/platform/jz4780-efuse/chip_id";
//        try
//        {
//            FileInputStream fin = new FileInputStream(fileName);
//            int length = fin.available();
//            byte buffer[] = new byte[length];
//            fin.read(buffer);
//            res = EncodingUtils.getString(buffer, "UTF-8").toString().trim();
//            fin.close();
//        }
//        catch(Exception e)
//        {
//            e.printStackTrace();
//        }
//        return res;
//    }

    public void shutDown()
    {
        execCommand("input keyevent 26");
    }

    public static void reboot()
    {
        execCommand("reboot");
    }

    public void back()
    {
        execCommand("input keyevent 4");
    }

    public void home()
    {
        execCommand("input keyevent 3");
    }

    private void exec(String command[])
        throws Exception
    {
        Process proc = Runtime.getRuntime().exec(command);
        proc.waitFor();
    }

    public static void execCommand(String command)
    {
        Process process = null;
        DataOutputStream os = null;
        try
        {
            process = Runtime.getRuntime().exec("su");
            os = new DataOutputStream(process.getOutputStream());
            os.writeBytes((new StringBuilder(String.valueOf(command))).append("\n").toString());
            os.writeBytes("exit\n");
            os.flush();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    private static Dwin dwin;

    /**
     * 关闭Android导航栏，实现全屏
     */
    public void closeBar() {
        try {
            String command;
            command = "LD_LIBRARY_PATH=/vendor/lib:/system/lib service call activity 42 s16 com.android.systemui";
            ArrayList<String> envlist = new ArrayList<String>();
            Map<String, String> env = System.getenv();
            for (String envName : env.keySet()) {
                envlist.add(envName + "=" + env.get(envName));
            }
            String[] envp = envlist.toArray(new String[0]);
            Process proc = Runtime.getRuntime().exec(
                    new String[]{"su", "-c", command}, envp);
            proc.waitFor();
        } catch (Exception ex) {
            // Toast.makeText(getApplicationContext(), ex.getMessage(),
            // Toast.LENGTH_LONG).show();
        }
    }

    /**
     * 显示导航栏
     */
    public  void showBar() {
        try {
            String command;
            command = "LD_LIBRARY_PATH=/vendor/lib:/system/lib am startservice -n com.android.systemui/.SystemUIService";
            ArrayList<String> envlist = new ArrayList<String>();
            Map<String, String> env = System.getenv();
            for (String envName : env.keySet()) {
                envlist.add(envName + "=" + env.get(envName));
            }
            String[] envp = envlist.toArray(new String[0]);
            Process proc = Runtime.getRuntime().exec(
                    new String[]{"su", "-c", command}, envp);
            proc.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
